import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/message/message_config.dart';
import 'package:ls_work/net/post_request.dart';
import 'package:ls_work/net/response/service_order_ddetail_data.dart';
import 'package:ls_work/util/ext_utils.dart';
import 'package:ls_work/viewmodel/service_order/service_order_detail_view_model.dart';
import 'package:ls_work/widgets/bottom_input_widget.dart';

class ReceiveServiceOrderViewModel extends BaseViewModel{
  ReceiveServiceOrderViewModel(BuildContext context,this.detailData) : super(context);

  ServiceOrderDdetailData detailData;


  void receiveOrderClick(){
    showApplyDialog(context: context, onSubmit: (){
      _receiveOrder();
    },message: '确定接单吗？');
  }

  Future<void> _receiveOrder() async {
    var dedata = ServiceOrderDetailViewModel.detailData!;
    showLoadingDialog(msg: '提交中...');
    try{
      var data = await PostRequest.receiveOrder(dedata.id.toString());
      hideDialog();
      if("0"==data['code'].toString()){
        showToast('接单成功');
        sendMessage(SendMessageEvent(MessageConfig.REFRESH_ORDER));
        finish();
      }else{
        showToast(data['msg'].toString());
      }
    }catch(e){
      hideDialog();
      print(e);
      showToast(NetErrorUtils.getNetError(e));
    }
  }

  void refuseOrderClick(){
    showBottomInput(context,onSubmit: (str){
      if(str.isNullOrEmpty()){
        showToast("请输入拒单原因");
      }
      _refuseOrder(str);
    },title: '拒单',hint: '请输入拒单原因');
  }

  Future<void> _refuseOrder(String reason) async {
    showLoadingDialog(msg: '提交中...');
    try{
      var data = await PostRequest.refuseOrder(detailData.id.toString(),reason);
      hideDialog();
      if("0"==data['code'].toString()){
        showToast('拒单成功');
        sendMessage(SendMessageEvent(MessageConfig.REFRESH_ORDER));
        finish();
        finish();
      }else{
        showToast(data['msg'].toString());
      }
    }catch(e){
      hideDialog();
      showToast(NetErrorUtils.getNetError(e));
    }
  }
}